/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "dma.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

#include "stm32g4xx_ll_gpio.h"
#include "stm32g4xx_ll_bus.h"
#include "AX58100hw.h"
#include "applinterface.h"
#include "adc_get.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint16_t toggle_led = 0;
uint16_t test_value = 0;
extern uint16_t ledtime;
extern uint16_t adctime;
extern uint32_t num[3];
extern DMA_HandleTypeDef hdma_usart1_rx;
extern DMA_HandleTypeDef hdma_usart2_rx;
extern DMA_HandleTypeDef hdma_adc1;
//extern DMA_HandleTypeDef hdma_usart1_rx;
//extern DMA_HandleTypeDef hdma_usart2_rx;
uint8_t data1[16]={0};
uint8_t data2[16]={0};

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
//void BOOT0_GPIO_Init(void);
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void LL_IT_Enble(void)
{
    LL_TIM_EnableCounter(TIM4);
    LL_TIM_EnableIT_UPDATE(TIM4);
    test_value += LL_EXTI_IsEnabledIT_0_31(LL_EXTI_LINE_0);
    test_value += LL_EXTI_IsEnabledIT_0_31(LL_EXTI_LINE_1) << 6;
    test_value += LL_EXTI_IsEnabledIT_0_31(LL_EXTI_LINE_2) << 7;
    test_value += LL_EXTI_IsEnabledIT_0_31(LL_EXTI_LINE_3) << 8;

}


/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{

  /* USER CODE BEGIN 1 */
   LL_Init1msTick(SystemCoreClock); 
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_SPI2_Init();
  MX_TIM4_Init();
  MX_TIM3_Init();
  MX_ADC1_Init();
  MX_USART1_UART_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
	__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
	__HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
    HAL_NVIC_DisableIRQ(DMA2_Channel1_IRQn);

//	HAL_UARTEx_ReceiveToIdle_IT(&huart1,data1,8);
//	HAL_UARTEx_ReceiveToIdle_IT(&huart2,data2,8);
//	__HAL_DMA_DISABLE_IT(&hdma_usart1_rx, DMA_IT_HT);
//	__HAL_DMA_DISABLE_IT(&hdma_usart2_rx, DMA_IT_HT);
//	__HAL_DMA_DISABLE_IT(&hdma_adc1, DMA_IT_HT);
    
	HAL_UART_Receive_DMA(&huart1,data1,16);
	HAL_UART_Receive_DMA(&huart2,data2,16);
    HAL_ADC_Start_DMA(&hadc1,(uint32_t *)num,3);

    LL_TIM_ClearFlag_UPDATE(TIM3);
    LL_TIM_EnableIT_UPDATE(TIM3);
    LL_TIM_DisableCounter(TIM3);

//    LL_TIM_SetCounter(TIM3, 0);
//    LL_TIM_EnableCounter(TIM3);

	 LL_SPI_Enable(SPI2);
	 LL_SYSTICK_EnableIT(); 
//     adc_get();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */


  while (1)
  {


//    LL_GPIO_TogglePin(GPIOB, LL_GPIO_PIN_8);    //测试BOOT0对应的引脚PB8的电平变化
//		LL_mDelay(500);
				HW_Init();        /* ��ʼ��������Ӳ����A/D��SPI�ڶ�д����*/ 
		 
				LL_IT_Enble();
				
				MainInit();        /* ��������ʼ������:ESC  ��COE   */ 
				
	//		LL_TIM4_Enable();
				 
				bRunApplication = TRUE;       /* ��������״̬��־*/  
      
				do  
			 { 
					 MainLoop();    /* ��ѭ��������ʵ�������Ժͷ����������ݽ���*/  
					 toggle_led++;
						if (toggle_led > 6000) {
								toggle_led = 0;
								LL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
						}
			 }  while (bRunApplication == TRUE);   /* ����Ƿ�������״̬*/     

				 HW_Release();      // ������ĺ���  


	


    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
//		return  0; 
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  LL_FLASH_SetLatency(LL_FLASH_LATENCY_4);
  while(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_4)
  {
  }
  LL_PWR_EnableRange1BoostMode();
  LL_RCC_HSE_Enable();
   /* Wait till HSE is ready */
  while(LL_RCC_HSE_IsReady() != 1)
  {
  }

  LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_3, 85, LL_RCC_PLLR_DIV_2);
  LL_RCC_PLL_EnableDomain_SYS();
  LL_RCC_PLL_Enable();
   /* Wait till PLL is ready */
  while(LL_RCC_PLL_IsReady() != 1)
  {
  }

  LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
  LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_2);
   /* Wait till System clock is ready */
  while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
  {
  }

  /* Insure 1us transition state at intermediate medium speed clock*/
  for (__IO uint32_t i = (170 >> 1); i !=0; i--);

  /* Set AHB prescaler*/
  LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
  LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
  LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
  LL_SetSystemCoreClock(170000000);

   /* Update the time base */
  if (HAL_InitTick (TICK_INT_PRIORITY) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
//void BOOT0_GPIO_Init(void) 
//{
//    // ʹ��GPIOBʱ��
//    LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB);

//    // ����PB8Ϊ���ģʽ
//    LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_8, LL_GPIO_MODE_OUTPUT);
//    LL_GPIO_SetPinOutputType(GPIOB, LL_GPIO_PIN_8, LL_GPIO_OUTPUT_PUSHPULL);
//    LL_GPIO_SetPinSpeed(GPIOB, LL_GPIO_PIN_8, LL_GPIO_SPEED_FREQ_HIGH);
//    LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_8, LL_GPIO_PULL_NO);  // Ӳ������ʱ�����ڲ�����

//    // ��ʼ�����������ŵ�ƽ
//    LL_GPIO_ResetOutputPin(GPIOB, LL_GPIO_PIN_8);
//}


/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
